﻿/*  PandoraUnleashed
    An open source API for the Pandora music service
    Copyright © 2011 Saulius Milevicius

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PandoraUnleashed.Data;

namespace PandoraUnleashed
{
    class Station
    {
        private Pandora pandora { get; set; }
        /// <summary>
        /// Current playlist
        /// </summary>
        public List<Track> playlist { get; set; }
        private Track currentTrack { get; set; }
        public StationData stationData { get; set; }

        /// <summary>
        /// Default constructor for a station
        /// </summary>
        /// <param name="pandora">Your instance of Pandora</param>
        /// <param name="stationData">The station data for the station</param>
        public Station(Pandora pandora, StationData stationData)
        {
            this.pandora = pandora;
            this.stationData = stationData;
            this.playlist = new List<Track>();
            loadPersistedTracks();
            if (this.playlist.Count > 0)
                currentTrack = playlist[0];
        }

        /// <summary>
        /// Load more tracks into the playlist
        /// </summary>
        public void fetchMoreTracks()
        {
            loadPersistedTracks();
            if (currentTrack == null)
                currentTrack = playlist[0];
        }

        /// <summary>
        /// Get the current track
        /// </summary>
        /// <returns>The current track</returns>
        public Track getCurrentTrack()
        {
            return currentTrack;
        }

        /// <summary>
        /// Remove the current track from the playlist and load in the next one
        /// </summary>
        public void getNextTrack()
        {
            playlist.RemoveAt(0);
            if (playlist.Count > 0)
                currentTrack = playlist[0];
            else
                currentTrack = null;
        }

        /// <summary>
        /// Load more tracks into the playlist
        /// </summary>
        private void loadPersistedTracks()
        {
            List<TrackData> trackData = pandora.getPlaylist(stationData);
            playlist.AddRange(makeTrackList(trackData));
        }

        /// <summary>
        /// Make a list of tracks from a list of track data
        /// </summary>
        /// <param name="trackData">List of track data</param>
        /// <returns>List of tracks</returns>
        private List<Track> makeTrackList(List<TrackData> trackData)
        {
            List<Track> tracks = new List<Track>();
            foreach (TrackData td in trackData)
            {
                if (td is AudioAdData)
                    tracks.Add(new AudioAd(pandora, (AudioAdData)td));
                else
                    tracks.Add(new Track(td));
            }
            return tracks;
        }
    }
}
